﻿using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using Cofe.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;

namespace Cofe.IO.Implements
{
#if !NETFX_CORE
    public class SystemIOWatcherEventSource : EventSource<CofeSystemEventArgs>
    {
        #region Constructor

        public SystemIOWatcherEventSource(IVolumeInfo volume, IParamParser paramParser)
        {
            _volumeId = volume.VolumeId;
            var dic = paramParser.StringToDictionary(volume.ParamString);
            if (!(dic.ContainsKey("VolumePath")))
                throw new ArgumentException("Volume path is not specified.");
            _rootPath = dic["VolumePath"];

            Func<string, string> convertPath = (fullFileSystemPath) =>
                {
                    return PathFE.Combine( "{" + _volumeId + "}",
                        fullFileSystemPath.Replace(_rootPath, "", StringComparison.InvariantCultureIgnoreCase));
                };


            _fileSystemWatcher = new System.IO.FileSystemWatcher(_rootPath);

         
            _fileSystemWatcher.IncludeSubdirectories = true;
            _fileSystemWatcher.Changed += new System.IO.FileSystemEventHandler(
                (o, e) =>
                {
                    dispatchEvent(CofeSystemEventArgs.FromParsePath(convertPath(e.FullPath), 
                        System.IO.Directory.Exists(e.FullPath),
                        Core.ModifiedAction.Changed));                    
                }
                );

            _fileSystemWatcher.Created += new System.IO.FileSystemEventHandler(
                (o, e) =>
                {
                    dispatchEvent(CofeSystemEventArgs.FromParsePath(convertPath(e.FullPath),
                        System.IO.Directory.Exists(e.FullPath),
                        Core.ModifiedAction.Created));        
                }
                );

            _fileSystemWatcher.Deleted += new System.IO.FileSystemEventHandler(
                (o, e) =>
                {
                    dispatchEvent(CofeSystemEventArgs.FromParsePath(convertPath(e.FullPath),
                        System.IO.Directory.Exists(e.FullPath),
                        Core.ModifiedAction.Deleted));        
                }
                );

            _fileSystemWatcher.Renamed += new System.IO.RenamedEventHandler(
                (o, e) =>
                {
                    dispatchEvent(CofeRenameEventArgs.FromParsePath(convertPath(e.OldFullPath), convertPath(e.FullPath),
                       System.IO.Directory.Exists(e.FullPath),
                       Core.ModifiedAction.Renamed));        
                }
                );

            try
            {
                _fileSystemWatcher.EnableRaisingEvents = true;
            }
            catch
            {

            }

        }
        
        #endregion

        #region Methods

        protected override void OnEnabled()
        {
            _fileSystemWatcher.EnableRaisingEvents = true;
            base.OnEnabled();
        }

        protected override void OnDisabled()
        {
            _fileSystemWatcher.EnableRaisingEvents = false;
            base.OnDisabled();
        }
        
        #endregion

        #region Data

        string _volumeId;
        string _rootPath;
        System.IO.FileSystemWatcher _fileSystemWatcher;
        
        #endregion

        #region Public Properties
        
        #endregion


    }
#endif
}
